package com.hailiao.beidou.message.ui.page.home

import androidx.activity.compose.BackHandler
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.paging.compose.LazyPagingItems
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.itemContentType
import androidx.paging.compose.itemKey
import com.d10ng.app.utils.goToSystemHome
import com.d10ng.compose.ui.AppColor
import com.d10ng.compose.ui.AppText
import com.d10ng.compose.ui.navigation.NavBar
import com.hailiao.beidou.message.R
import com.hailiao.beidou.message.data.chat.ChatRepository
import com.hailiao.beidou.message.data.db.entity.ChatEntity
import com.hailiao.beidou.message.data.db.entity.ChatWithContactEntity
import com.hailiao.beidou.message.ui.page.destinations.ChatScreenDestination
import com.hailiao.beidou.message.ui.page.destinations.ContactsScreenDestination
import com.hailiao.beidou.message.ui.page.destinations.SearchScreenDestination
import com.hailiao.beidou.message.ui.page.destinations.SettingScreenDestination
import com.hailiao.beidou.message.ui.page.home.view.ChatCell
import com.hailiao.beidou.message.ui.view.BDStatusBar
import com.hailiao.beidou.message.ui.view.NavBarIcon
import com.hailiao.beidou.message.utils.go
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootNavGraph
import kotlinx.coroutines.launch

/**
 * 首页
 * @Author d10ng
 * @Date 2024/3/1 17:02
 */
@RootNavGraph
@Destination(style = HomePageTransitions::class)
@Composable
fun HomeScreen(
    model: HomeScreenViewModel = viewModel()
) {
    val data = model.dataFlow.collectAsLazyPagingItems()
    val scope = rememberCoroutineScope()

    HomeScreenView(
        data = data,
        onClickSearch = {
            go(SearchScreenDestination.route)
        },
        onClickContacts = {
            go(ContactsScreenDestination.route)
        },
        onClickSetting = {
            go(SettingScreenDestination.route)
        },
        onClickChat = {
            go(ChatScreenDestination(it).route)
            // 清空未读消息数量标记
            scope.launch {
                ChatRepository.clearUnreadCount(it)
            }
        },
        onClickChatDelete = {
            model.onClickDeleteChat(it)
        }
    )

    BackHandler {
        goToSystemHome()
    }
}

@Composable
private fun HomeScreenView(
    data: LazyPagingItems<ChatWithContactEntity>,
    onClickSearch: () -> Unit = {},
    onClickContacts: () -> Unit = {},
    onClickSetting: () -> Unit = {},
    onClickChat: (String) -> Unit = {},
    onClickChatDelete: (ChatEntity) -> Unit = {}
) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(AppColor.Neutral.bg)
            .navigationBarsPadding()
    ) {
        NavBar(
            title = stringResource(id = R.string.app_name),
            titleAlignment = Alignment.CenterStart,
            right = {
                NavBarIcon(iconId = R.drawable.home_search, onClick = onClickSearch)
                NavBarIcon(iconId = R.drawable.home_contacts, onClick = onClickContacts)
                NavBarIcon(iconId = R.drawable.home_setting, onClick = onClickSetting)
            }
        )
        BDStatusBar()
        Box(
            modifier = Modifier
                .fillMaxSize()
                .weight(1f)
        ) {
            LazyColumn(
                modifier = Modifier
                    .fillMaxSize(),
                horizontalAlignment = Alignment.End
            ) {
                items(
                    count = data.itemCount,
                    key = data.itemKey(),
                    contentType = data.itemContentType()
                ) { index ->
                    data[index]?.let { item ->
                        ChatCell(
                            value = item,
                            longPress = true,
                            onClick = {
                                onClickChat(item.chat.number)
                            },
                            onClickDelete = {
                                onClickChatDelete(item.chat)
                            }
                        )
                    }
                }
            }
            if (data.itemCount == 0) {
                EmptyContent()
            }
        }
    }
}

@Composable
private fun EmptyContent() {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(19.dp),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Image(
            painter = painterResource(id = R.drawable.contact_empty),
            contentDescription = null,
            modifier = Modifier.size(124.dp),
            contentScale = ContentScale.Fit
        )
        Text(
            text = "暂无信息",
            style = AppText.Normal.Tips.default,
            textAlign = TextAlign.Center,
            modifier = Modifier.padding(top = 19.dp)
        )
    }
}